package com.osmino.contacts.icon;

import java.util.ArrayList;
import java.util.List;
import com.osmino.contacts.groups.edit.GroupIconItem;
import android.content.Context;
import android.content.res.Resources;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public final class GroupIconAdapter extends BaseAdapter {

	private final Context mContext;
	private final List<Integer> mItems;
	private static final int sSize = 154;
	private static final String sDrawableString = "drawable";

	public GroupIconAdapter(Context context) {
		mContext = context;
		mItems = new ArrayList<Integer>(sSize);
		Resources res = mContext.getResources();
		String packageName = mContext.getPackageName();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < sSize; i++) {
			sb.delete(0, sb.length());
			sb.append("group_ic_").append(i + 1);
			int drawableResource = res.getIdentifier(sb.toString(), sDrawableString, packageName);
			mItems.add(drawableResource);
		}
	}

	@Override
	public int getCount() {
		return mItems.size();
	}

	@Override
	public Integer getItem(int position) {
		return mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			view = newView(position, parent);
		}
		bindView(position, view);
		return view;
	}
	
	private View newView(int position, ViewGroup parent) {
		return new GroupIconItem(mContext);
	}
	
	private void bindView(int position, View view) {
		GroupIconItem iconView = (GroupIconItem) view;
		iconView.setResourceAndLabel(mItems.get(position), null);
	}
}
